package com.hunliji.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.common.collect.Lists;
import com.hunliji.monitor.dao.mapper.mt.AlertChatBindDAO;
import com.hunliji.monitor.dao.model.dataobject.AlertChatBindDO;
import com.hunliji.monitor.dao.model.dataobject.AlertConfigDO;
import com.hunliji.monitor.service.api.AlertChatBindService;
import com.hunliji.monitor.service.api.model.bo.AlertChatBindBO;
import com.hunliji.monitor.service.api.model.bo.AlertConfigBO;
import com.hunliji.monitor.share.component.convertor.ConverterFacade;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * File：AlertChatBindServiceImpl.java<br> Title: <br> Description: <br>
 *
 * @author lejin Date: 2022/10/10
 */
@Service
public class AlertChatBindServiceImpl implements AlertChatBindService {

    @Resource
    AlertChatBindDAO alertChatBindDAO;

    @Resource
    ConverterFacade converterFacade;

    @Override
    public List<AlertChatBindBO> listByQuery(Long alertConfigId, Long chatGroupId) {
        LambdaQueryWrapper<AlertChatBindDO> queryWrapper = new LambdaQueryWrapper<AlertChatBindDO>()
            .eq(Objects.nonNull(alertConfigId), AlertChatBindDO::getAlertConfigId, alertConfigId)
            .eq(Objects.nonNull(chatGroupId), AlertChatBindDO::getChatGroupId, chatGroupId)
            .eq(AlertChatBindDO::getDeleted, Boolean.FALSE);
        List<AlertChatBindDO> list = alertChatBindDAO.selectList(queryWrapper);
        return converterFacade.convertList(list, AlertChatBindBO.class);
    }

    @Override
    public AlertChatBindBO getById(Long id) {
        if(Objects.isNull(id)){
            return null;
        }
        AlertChatBindDO alertChatBindDO = alertChatBindDAO.selectById(id);
        return converterFacade.convert(alertChatBindDO, AlertChatBindBO.class);
    }

    @Override
    public void insert(AlertChatBindBO alertChatBindBO) {
        if(Objects.isNull(alertChatBindBO)){
            return;
        }
        AlertChatBindDO alertChatBindDO = converterFacade.convert(alertChatBindBO,
            AlertChatBindDO.class);
        alertChatBindDAO.insert(alertChatBindDO);
    }

    @Override
    public void update(AlertChatBindBO alertChatBindBO) {
        if(Objects.isNull(alertChatBindBO)){
            return;
        }
        AlertChatBindDO alertChatBindDO = converterFacade.convert(alertChatBindBO,
            AlertChatBindDO.class);
        alertChatBindDAO.updateById(alertChatBindDO);
    }

    @Override
    public void deleteByAlertConfigId(Long alertConfigId) {
        if(Objects.isNull(alertConfigId)){
            return;
        }
        LambdaUpdateWrapper<AlertChatBindDO> updateWrapper = new LambdaUpdateWrapper<AlertChatBindDO>()
            .eq(AlertChatBindDO::getAlertConfigId, alertConfigId)
            .set(AlertChatBindDO::getDeleted, Boolean.TRUE);
        alertChatBindDAO.update(null, updateWrapper);
    }

    @Override
    public void deleteById(Long id) {
        if(Objects.isNull(id)){
            return;
        }
        LambdaUpdateWrapper<AlertChatBindDO> updateWrapper = new LambdaUpdateWrapper<AlertChatBindDO>()
            .eq(AlertChatBindDO::getId, id)
            .set(AlertChatBindDO::getDeleted, Boolean.TRUE);
        alertChatBindDAO.update(null, updateWrapper);
    }
}
